

import Taro from '@tarojs/taro'
import Utils from './utils'
import Global from './global'

const HTTP_STATUS = {
  SUCCESS: 200,
  CLIENT_ERROR: 400,
  AUTHENTICATE: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  SERVER_ERROR: 500,
  BAD_GATEWAY: 502,
  SERVICE_UNAVAILABLE: 503,
  GATEWAY_TIMEOUT: 504
}

export default {
  setToken:token=>Global.set('token',token),
  baseOptions(params, method = 'GET') {
    let { url, data } = params
    let token = Global.get('token')
    let contentType = 'application/json; charset=utf-8'
    contentType = params.contentType || contentType
    //Utils.debug(data)
    const option = {
      isShowLoading: false,
      loadingText: '正在加载',
      url: url,
      data: data,
      method: method,
      header: {
        'Accept': 'application/json',
        'content-type': contentType,
        'token': token },
      success(res) {
        if (res.statusCode === HTTP_STATUS.SUCCESS) {
          const data = res.data
          Utils.debug('res',res)
          let err = ''
          if (data.status === HTTP_STATUS.NOT_FOUND) {
            err = '请求资源不存在'
          } else if (data.status === HTTP_STATUS.AUTHENTICATE) {
            err = '未登录，请先登录'
          } else if (data.status === HTTP_STATUS.FORBIDDEN) {
            err = '没有权限访问'
          } else if (data.status === HTTP_STATUS.SERVER_ERROR) {
            err = '服务器错误'
          }
          if(data.status != HTTP_STATUS.SUCCESS){
            Utils.error('接口异常', params.title+':'+err+','+data.message)
            Taro.showModal({
              title: '接口异常',
              content: err,
              success: r => {
                if (data.status === HTTP_STATUS.AUTHENTICATE){
                  if (r.confirm) {
                    //登录
                    Taro.redirectTo({
                      url:'../pages/login/login'
                    })
                  } else if (r.cancel) {
                    //回退
                    Taro.navigateBack()
                  }
                }
              }
            })
          }
          else{
            return data.list
          }
        }
        else{
          Utils.debug(res)
        }
      },
      error(e) {
        Taro.showModal({
          title:'请求接口出现问题',
          content:e.message
        })
        Utils.error('api', '请求接口出现问题', e)
      }
    }
    return Taro.request(option)
  },
  get(url, data = '') {
    let option = { url, data }
    return this.baseOptions(option)
  },
  post: function (url, data, contentType='application/json') {
    let params = { url, data, contentType }
    return this.baseOptions(params, 'POST')
  }
  ,
  put: function (url, data, contentType='application/json') {
    let params = { url, data, contentType }
    return this.baseOptions(params, 'PUT')
  }
  ,
  del: function (url, data, contentType='application/json') {
    let params = { url, data, contentType }
    return this.baseOptions(params, 'DELETE')
  },
  request: function (params, data = {}, method = 'POST'){
    const p = data == {} ? params : {...params,data:data}
    return this.baseOptions(p, p.method ? p.method : method)
  }
}
